package exam.test15;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI implements ActionListener {
    private JButton addStudentButton, quitButton;
    private JLabel label1, label2, label3;
    private JTextField nameTextField;
    private JTextArea outputTextArea;
    private String classType;
    private JFrame jf;
    private JScrollPane scrollPane;
    private JRadioButton rbDay, rbEvening;
    private ClassList classList = new ClassList();

    public GUI() {

        jf = new JFrame("Student Enrollment");
        JPanel northPanel = createNorthPanel();
        jf.add(northPanel, BorderLayout.NORTH);
        JPanel radioButtonsPanel = createClassTypeRadioButtons();
        jf.add(radioButtonsPanel, BorderLayout.CENTER);
        JPanel southPanel = createSouthPanel();
        jf.add(southPanel, BorderLayout.SOUTH);
        jf.setVisible(true);
        jf.setSize(500, 300);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new GUI();
    }

    // create north panel – label and name text field and common.stack.delayStack.two buttons.
    public JPanel createNorthPanel() {
        // add the java code to create the label, name text field and common.stack.delayStack.two buttons ("addStudentButton" & "quitButton") with the listener
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(2, 2));

        label1 = new JLabel("Enter Student Name");
        jPanel.add(label1);

        nameTextField = new JTextField(20);
        jPanel.add(nameTextField);

        addStudentButton = new JButton("addStudentButton");
        quitButton = new JButton("quitButton");
        addStudentButton.addActionListener(this);
        quitButton.addActionListener(this);

        jPanel.add(addStudentButton);
        jPanel.add(quitButton);

        return jPanel;
    }

    public JPanel createClassTypeRadioButtons() {
        //Add the java code to create the radio buttons, and register the listener
        JPanel jPanel = new JPanel();
        label2 = new JLabel("Enter the class type");
        rbDay = new JRadioButton("Day", true);
        rbEvening = new JRadioButton("Evening", false);
        classType = "Day";
        rbDay.addActionListener(this);
        rbEvening.addActionListener(this);

        jPanel.add(label2);
        jPanel.add(rbDay);
        jPanel.add(rbEvening);
        return jPanel;
    }

    public JPanel createSouthPanel() {
        outputTextArea = new JTextArea("common.list of Enrolments will appear here");
        outputTextArea.setColumns(30);
        outputTextArea.setRows(5);
        scrollPane = new JScrollPane(outputTextArea);
        JPanel aPanel = new JPanel();
        aPanel.add(scrollPane);
        return aPanel;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        // Add the code to handle the events of the buttons ( addStudentButton, quitButton, rbDay and rbEvening; )
        //The addStudentButton  add ane student to the enolemnt common.list n the ClassList  class  and  output current enrolment common.list to the output text area field

        if (ae.getSource().equals(addStudentButton)) {
            String studentName = nameTextField.getText();
            classList.addStudent(studentName, classType);
            outputTextArea.setText(classList.toString());

        }else  if (ae.getSource().equals(quitButton)) {
            System.exit(0);
        }else if (ae.getSource().equals(rbDay)) {
            rbEvening.setSelected(false);
            classType = "Day";
        }else if (ae.getSource().equals(rbEvening)) {
            rbDay.setSelected(false);
            classType = "Evening";
        }
    }


}

